package com.ruoyi.LostDevices.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.model.LoginUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.LostDevices.domain.LostDevices;
import com.ruoyi.LostDevices.service.ILostDevicesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import static com.ruoyi.common.utils.SecurityUtils.getAuthentication;

/**
 * 遗失物品Controller
 *
 * @author ruoyi
 * @date 2025-03-19
 */
@RestController
@RequestMapping("/LostDevices/LostDevices")
public class LostDevicesController extends BaseController
{
    @Autowired
    private ILostDevicesService lostDevicesService;

    /**
     * 查询遗失物品列表
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(LostDevices lostDevices)
    {
        LoginUser user = (LoginUser) getAuthentication().getPrincipal();
        System.out.println(user.toString());
        startPage();
        List<LostDevices> list = lostDevicesService.selectLostDevicesList(lostDevices);
        return getDataTable(list);
    }

    /**
     * 导出遗失物品列表
     */
    @Anonymous
    @Log(title = "遗失物品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LostDevices lostDevices)
    {
        List<LostDevices> list = lostDevicesService.selectLostDevicesList(lostDevices);
        ExcelUtil<LostDevices> util = new ExcelUtil<LostDevices>(LostDevices.class);
        util.exportExcel(response, list, "遗失物品数据");
    }

    /**
     * 获取遗失物品详细信息
     */
    @Anonymous
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(lostDevicesService.selectLostDevicesById(id));
    }

    /**
     * 新增遗失物品
     */
    @Anonymous
    @Log(title = "遗失物品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LostDevices lostDevices)
    {
        LoginUser user = (LoginUser) getAuthentication().getPrincipal();
        System.out.println(user.toString());
        lostDevices.setReporter(user.getUsername());
        lostDevices.setStatus("2");
        return toAjax(lostDevicesService.insertLostDevices(lostDevices));
    }

    /**
     * 修改遗失物品
     */
    @Anonymous
    @Log(title = "遗失物品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LostDevices lostDevices)
    {
        LoginUser user = (LoginUser) getAuthentication().getPrincipal();
        System.out.println(user.toString());
        lostDevices.setResolveBy(user.getUsername());
        return toAjax(lostDevicesService.updateLostDevices(lostDevices));
    }

    /**
     * 删除遗失物品
     */
    @Anonymous
    @Log(title = "遗失物品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(lostDevicesService.deleteLostDevicesByIds(ids));
    }
}
